package com.gaomall.dubbo.service.user.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.gaomall.dubbo.commons.domain.TbUser;
import com.gaomall.dubbo.service.user.api.UserService;
import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "user")
public class UserController {

    // 注册中心引用服务
    @Reference(version = "${services.versions.user.v1}")
    private UserService userService;

    @HystrixCommand(fallbackMethod = "listError")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(Model model) {

//        List<TbUser> tbUsers = userService.selectAll();
//        model.addAttribute("tbUsers", tbUsers);

        PageInfo<TbUser> pageInfo = userService.page(
                1, 2);
//        pageInfo.getList().size();
        model.addAttribute("pageInfo", pageInfo);
        return "user/list";
    }

    /**
     * 熔断方法
     * @return
     */
    public String listError() {
        return "Hystrix fallback";
    }

}
